package com.xiaoxing.salesclient.mvp.ui.activity;

import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.StaggeredGridLayoutManager;
import android.util.Log;
import android.view.View;
import android.widget.TextView;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.haibin.calendarview.CalendarLayout;
import com.haibin.calendarview.CalendarView;
import com.jess.arms.base.BaseActivity;
import com.jess.arms.di.component.AppComponent;
import com.jess.arms.utils.ArmsUtils;
import com.xiaoxing.salesclient.di.component.DaggerKeChengBiaoComponent;
import com.xiaoxing.salesclient.di.module.KeChengBiaoModule;
import com.xiaoxing.salesclient.mvp.contract.KeChengBiaoContract;
import com.xiaoxing.salesclient.mvp.presenter.KeChengBiaoPresenter;
import com.xiaoxing.salesclient.mvp.ui.adapter.DotTimeLineAdapter;
import com.xiaoxing.salesclient.mvp.ui.entity.Event;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;
import me.jessyan.armscomponent.commonres.utils.ToolbarUtils;
import me.jessyan.armscomponent.commonres.view.timelineitemdecoration.itemdecoration.DotItemDecoration;
import me.jessyan.armscomponent.commonres.view.timelineitemdecoration.itemdecoration.SpanIndexListener;
import me.jessyan.armscomponent.commonsdk.core.RouterHub;
import xiaoxing.com.salesclient.R;
import xiaoxing.com.salesclient.R2;

import static com.jess.arms.utils.Preconditions.checkNotNull;


@Route(path = RouterHub.SALES_CLIENT_KE_CHENG_BIAO_ACTIVITY)
public class KeChengBiaoActivity extends BaseActivity<KeChengBiaoPresenter> implements KeChengBiaoContract.View {


    @BindView(R2.id.calendarLayout)
    CalendarLayout mCalendarLayout;

    @BindView(R2.id.calendarView)
    CalendarView mCalendarView;
    @BindView(R2.id.tv_shang_yi_zhou)
    TextView tvShangYiZhou;
    @BindView(R2.id.tv_year)
    TextView tvYear;
    @BindView(R2.id.tv_xia_yi_zhou)
    TextView tvXiaYiZhou;

    RecyclerView mRecyclerView;

    List<Event> mList = new ArrayList<>();
    DotTimeLineAdapter mAdapter;
    DotItemDecoration mItemDecoration;

    long[] times = {
            1497229200,
            1497240000,
            1497243600,
            1497247200,
            1497249000,
            1497252600
    };
    String[] events = new String[]{
            "2018-08-20",
            "阿美族舞蹈",
            "2018-08-20",
            "阿美族舞蹈",
            "2018-08-20",
            "阿美族舞蹈"
    };


    @Override
    public void setupActivityComponent(@NonNull AppComponent appComponent) {
        DaggerKeChengBiaoComponent //如找不到该类,请编译一下项目
                .builder()
                .appComponent(appComponent)
                .keChengBiaoModule(new KeChengBiaoModule(this))
                .build()
                .inject(this);
    }

    @Override
    public int initView(@Nullable Bundle savedInstanceState) {
        return R.layout.activity_ke_cheng_biao; //如果你不需要框架帮你设置 setContentView(id) 需要自行设置,请返回 0
    }

    @Override
    public void initData(@Nullable Bundle savedInstanceState) {

        ToolbarUtils.initToolbarTitleBack(this, "課程表");

        mRecyclerView = (RecyclerView) findViewById(R.id.recyclerView);
        mRecyclerView.setLayoutManager(new StaggeredGridLayoutManager(2, StaggeredGridLayoutManager.VERTICAL));
        mItemDecoration = new DotItemDecoration
                .Builder(this)
                .setOrientation(DotItemDecoration.VERTICAL)//if you want a horizontal item decoration,remember to set horizontal orientation to your LayoutManager
                .setItemStyle(DotItemDecoration.STYLE_RESOURCE)
                .setLineStyle(DotItemDecoration.STYLE_DRAW)
                .setTopDistance(50)//dp
                .setItemInterVal(50)//dp
                .setItemPaddingLeft(20)//default value equals to item interval value
                .setItemPaddingRight(20)//default value equals to item interval value
                .setDotRes(R.drawable.icon_ke_cheng_biao_dot)
                .setDotRadius(2)//dp
                .setDotPaddingTop(0)
                .setDotInItemOrientationCenter(false)//set true if you want the dot align center
                .setLineRes(R.drawable.icon_ke_cheng_biao_vertical_line)
                .setLineColor(Color.RED)
                .setLineWidth(2)//dp
                .setEndText("END")
                .setTextColor(Color.WHITE)
                .setTextSize(10)//sp
                .setDotPaddingText(2)//dp.The distance between the last dot and the end text
                .setBottomDistance(40)//you can add a distance to make bottom line longer
                .create();
        mItemDecoration.setSpanIndexListener(new SpanIndexListener() {
            @Override
            public void onSpanIndexChange(View view, int spanIndex) {
                Log.i("Info", "view:" + view + "  span:" + spanIndex);
                view.setBackgroundResource(spanIndex == 0 ? R.drawable.icon_ke_cheng_biao_time_line_left : R.drawable.icon_ke_cheng_biao_time_line_right);
            }
        });
        mRecyclerView.addItemDecoration(mItemDecoration);

        for (int i = 0; i < times.length; i++) {
            Event event = new Event();
            event.setTime(times[i]);
            event.setEvent(events[i]);
            mList.add(event);
        }

        mAdapter = new DotTimeLineAdapter(this, mList);
        mRecyclerView.setAdapter(mAdapter);
    }

    @Override
    public void showLoading() {

    }

    @Override
    public void hideLoading() {

    }

    @Override
    public void showMessage(@NonNull String message) {
        checkNotNull(message);
        ArmsUtils.snackbarText(message);
    }

    @Override
    public void launchActivity(@NonNull Intent intent) {
        checkNotNull(intent);
        ArmsUtils.startActivity(intent);
    }

    @Override
    public void killMyself() {
        finish();
    }


    @OnClick({R2.id.tv_shang_yi_zhou, R2.id.tv_year, R2.id.tv_xia_yi_zhou})
    public void onClick(View view) {
        int i = view.getId();
        if (i == R.id.tv_shang_yi_zhou) {
            mCalendarView.scrollToPre(true);
        } else if (i == R.id.tv_year) {
            if (mCalendarLayout.isExpand()) {
                mCalendarLayout.shrink();
            } else {
                mCalendarLayout.expand();
            }
        } else if (i == R.id.tv_xia_yi_zhou) {
            mCalendarView.scrollToNext(true);
        }
    }
}
